@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 一键构建本地数据库脚本 (Windows版本)
REM 支持MySQL和PostgreSQL

echo ========================================
echo    中山大学研究生院校企对接平台
echo        数据库一键构建脚本
echo ========================================
echo.

REM 设置颜色
set GREEN=[92m
set RED=[91m
set YELLOW=[93m
set BLUE=[94m
set NC=[0m

REM 检查Docker是否安装
echo %BLUE%检查Docker环境...%NC%
docker --version >nul 2>&1
if errorlevel 1 (
    echo %RED%错误: Docker未安装或未启动，请先安装Docker Desktop%NC%
    echo 下载地址: https://www.docker.com/products/docker-desktop
    pause
    exit /b 1
)

echo %GREEN%Docker环境检查通过%NC%
echo.

REM 检查Docker Compose是否可用
docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo %RED%错误: Docker Compose不可用%NC%
    pause
    exit /b 1
)

echo %GREEN%Docker Compose检查通过%NC%
echo.

REM 选择数据库类型
echo %YELLOW%请选择要构建的数据库类型:%NC%
echo 1. MySQL (推荐)
echo 2. PostgreSQL
echo 3. 仅启动数据库服务 (MySQL + Redis)
echo 4. 启动完整环境 (MySQL + Redis + 应用)
echo 5. 清理所有数据并重新构建
echo.
set /p choice="请输入选择 (1-5): "

if "%choice%"=="1" goto mysql_setup
if "%choice%"=="2" goto postgres_setup
if "%choice%"=="3" goto db_only
if "%choice%"=="4" goto full_setup
if "%choice%"=="5" goto clean_setup
echo %RED%无效选择%NC%
pause
exit /b 1

:mysql_setup
echo.
echo %BLUE%开始构建MySQL数据库环境...%NC%
goto start_services

:postgres_setup
echo.
echo %BLUE%开始构建PostgreSQL数据库环境...%NC%
echo %YELLOW%注意: 需要修改docker-compose.yml中的数据库配置%NC%
goto start_services

:db_only
echo.
echo %BLUE%启动数据库服务 (MySQL + Redis)...%NC%
docker-compose up -d mysql redis
if errorlevel 1 (
    echo %RED%数据库服务启动失败%NC%
    pause
    exit /b 1
)
goto wait_services

:full_setup
echo.
echo %BLUE%启动完整环境 (MySQL + Redis + 应用)...%NC%
docker-compose up -d
if errorlevel 1 (
    echo %RED%完整环境启动失败%NC%
    pause
    exit /b 1
)
goto wait_services

:clean_setup
echo.
echo %YELLOW%警告: 这将删除所有数据并重新构建%NC%
set /p confirm="确认继续? (y/N): "
if /i not "%confirm%"=="y" (
    echo 操作已取消
    pause
    exit /b 0
)

echo %BLUE%停止并删除所有容器和卷...%NC%
docker-compose down -v
docker system prune -f

echo %BLUE%重新构建环境...%NC%
docker-compose up -d --build
if errorlevel 1 (
    echo %RED%重新构建失败%NC%
    pause
    exit /b 1
)
goto wait_services

:start_services
echo %BLUE%启动数据库服务...%NC%
docker-compose up -d mysql redis
if errorlevel 1 (
    echo %RED%数据库服务启动失败%NC%
    pause
    exit /b 1
)

:wait_services
echo.
echo %BLUE%等待服务启动完成...%NC%
timeout /t 10 /nobreak >nul

REM 检查MySQL服务状态
echo %BLUE%检查MySQL服务状态...%NC%
docker exec sysu-giep-mysql mysqladmin ping -h localhost --silent
if errorlevel 1 (
    echo %YELLOW%MySQL服务可能还在启动中，请稍等...%NC%
    timeout /t 15 /nobreak >nul
    docker exec sysu-giep-mysql mysqladmin ping -h localhost --silent
    if errorlevel 1 (
        echo %RED%MySQL服务启动失败%NC%
        pause
        exit /b 1
    )
)

echo %GREEN%MySQL服务运行正常%NC%

REM 检查Redis服务状态
echo %BLUE%检查Redis服务状态...%NC%
docker exec sysu-giep-redis redis-cli ping >nul 2>&1
if errorlevel 1 (
    echo %RED%Redis服务启动失败%NC%
    pause
    exit /b 1
)

echo %GREEN%Redis服务运行正常%NC%

REM 创建数据库和表
echo.
echo %BLUE%初始化数据库结构...%NC%

REM 检查是否有Go环境
go version >nul 2>&1
if errorlevel 1 (
    echo %YELLOW%Go环境未找到，跳过数据库迁移%NC%
    echo %YELLOW%请手动运行: go run cmd/server/main.go%NC%
    goto show_info
)

REM 设置环境变量
set DATABASE_URL=mysql://root:password@localhost:3306/sysu_giep?charset=utf8mb4&parseTime=True&loc=Local

echo %BLUE%运行数据库迁移...%NC%
go run cmd/server/main.go &
set APP_PID=%!

REM 等待应用启动
timeout /t 5 /nobreak >nul

REM 检查应用是否启动成功
curl -s http://localhost:8080/api/v1/health >nul 2>&1
if errorlevel 1 (
    echo %YELLOW%应用可能还在启动中...%NC%
    timeout /t 10 /nobreak >noul
)

REM 停止应用进程
taskkill /F /PID %APP_PID% >nul 2>&1

:show_info
echo.
echo %GREEN%========================================%NC%
echo %GREEN%        数据库构建完成！%NC%
echo %GREEN%========================================%NC%
echo.
echo %BLUE%服务信息:%NC%
echo MySQL:     localhost:3306
echo Redis:     localhost:6379
echo 数据库名:   sysu_giep
echo 用户名:     root
echo 密码:       password
echo.
echo %BLUE%管理命令:%NC%
echo 查看日志:   docker-compose logs -f mysql
echo 进入MySQL: docker exec -it sysu-giep-mysql mysql -u root -p
echo 进入Redis: docker exec -it sysu-giep-redis redis-cli
echo 停止服务:   docker-compose down
echo 重启服务:   docker-compose restart
echo.
echo %BLUE%下一步:%NC%
echo 1. 运行应用: go run cmd/server/main.go
echo 2. 访问API: http://localhost:8080/api/v1/health
echo 3. 查看文档: http://localhost:8080/swagger/index.html
echo.

REM 询问是否启动应用
set /p start_app="是否现在启动应用? (y/N): "
if /i "%start_app%"=="y" (
    echo %BLUE%启动应用服务...%NC%
    go run cmd/server/main.go
)

pause
